// pages/chart/chart.js

import { FindMessagesByMemberConversationId } from '../../api/message.js';
import { EventBusInstance } from '../../utils/eventBus.js';


const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    conversationId: null,
    memberConversationId: null,
    memberConversationName: null,
    sendText: '',
    sendBtnDisable: true,
    messageList: [
    ],
    buttom: null,
    currentUserInfo: null,
    windowHeight: null,
    statusBarHeight: null,
    headerHeight: null,
    footHeight: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initContentHeight();


    this.setData({
      currentUserInfo: app.globalData.currentUserInfo,
      conversationId: Number(options.conversationId),
      memberConversationId: Number(options.memberConversationId),
      memberConversationName: options.memberConversationName
    });

    FindMessagesByMemberConversationId({ memberConversationId: this.data.memberConversationId })
    .then(resp => {
      console.log(resp);
      this.setData({
        messageList: resp.content,
        buttom: `item${resp.content.length - 1}`
      });
    })
    .catch(err => {
      console.log(err);
      wx.showToast({ title: '无法获取消息列表', icon: 'none', duration: 2000 });
    });

    EventBusInstance.on('chat', data => {
      console.log(data);
      if (this.data.conversationId == data.conversationId) {
        this.data.messageList.push(data);
        this.setData({
          messageList: this.data.messageList,
          buttom: `item${this.data.messageList.length - 1}`
        });
      }
    }, true);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   buttom: `item${this.data.messageList.length - 1}`
    // });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  initContentHeight() {
    const windowHeight = wx.getSystemInfoSync().windowHeight;
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;

    this.setData({
      windowHeight: windowHeight,
      statusBarHeight: statusBarHeight
    });


    wx.createSelectorQuery().select('#header').boundingClientRect(rect => {
      this.setData({
        headerHeight: rect.height
      });
    }).exec();

    wx.createSelectorQuery().select('#foot').boundingClientRect(rect => {
      this.setData({
        footHeight: rect.height
      });
    }).exec();
  },

  sendMessageInput(e) {
    const value = e.detail.value;
    this.setData({
      sendText: value,
      sendBtnDisable: value.length === 0
    });
  },

  sendMessage(e) {
    if (!app.globalData.websocketConnected) {
      wx.showToast({ title: '网络拥堵，请稍后', icon: 'none', duration: 2000 });
      return;
    }

    wx.sendSocketMessage({
      data: JSON.stringify({
        conversationId: this.data.conversationId,
        memberConversationId: this.data.memberConversationId,
        senderId: app.globalData.currentUserInfo.id,
        messageType: 'TEXT',
        messageBody: this.data.sendText
      }),
    });
    
    this.setData({
      sendText: '',
      sendBtnDisable: true,
    });
  },

  toHome() {
    wx.navigateBack({
      delta: 1
    });
  }

})